<?php
    $modelName = $TableName;
    $hasil = $modelName;
    $cari = $modelName;

    foreach(glob(Yii::getPathOfAlias('application.models').'/*.php') as $filename){
        $arrModelPath = explode("/", $filename);
        $strModelName = $arrModelPath[count($arrModelPath)-1];
        $strModelName = str_replace(".php", "", $strModelName);
        
        if($hasil==$strModelName)
            break;
        
        $funct = get_class_methods($strModelName);
        if(is_int(array_search('tableName', $funct))){
            if ($strModelName::model()->tableName() == $cari){
                $hasil = $strModelName;
            }
        }else{
            continue;
        }
    }
    $modelName = $hasil;
    
    $mod = new $modelName;

    if (!isset($arrColumns)){
        $arrColumns = array();
        $jumlahCol = 0;
        foreach ($mod->tableSchema->columns as $column){
            if($column->name == 'CreatedBy' || $column->name == 'CreatedOn' || $column->name == 'LastUpdateBy'|| $column->name == 'LastUpdateOn')
            continue;
            array_push($arrColumns, $column->name);
            $jumlahCol +=1;
            if ($jumlahCol>5)
                break;
        }
    }

    $model = null;
    $model = 'model_'.$modelName;
    $model = new $modelName('search');

    $model->unsetAttributes();  // clear any default values
    if(isset($_GET[$modelName]))
            $model->attributes=$_GET[$modelName];
        
    if(!isset($modelProvider)){
        $modelProvider = $model->search();
    }
    
    $aCol = array(
                array(
                    'type'=>'raw',
                    'value'=> "CHtml::button('pilih', array(
                        'id'=>'btn_select_$modelName',
                        'onClick'=>\"setData('$targetElement', '\$data->$ColumnName', '$targetElementLabel', '\$data->$targetValueLabel','$dialogName')\",
                        ))",
                )
            );
            
    foreach ($arrColumns as $col){
        array_push($aCol, $col); 
    }
    $arrColumns = $aCol;
    
    $this->widget('zii.widgets.grid.CGridView', array(
	'id'=>'model_'.$targetElement.'-grid',
	'dataProvider'=>$modelProvider,
	'filter'=>$model,
	'columns'=>$arrColumns
    ));
?>